<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.LinkedList"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Enumeration"%>

<%
class TurnPage {
	LinkedList<HashMap<String,String>> userVisitUrl = null;
	private String GdUserVisitUrlList = "KdUserVisitUrlList";
	HttpServletRequest request = null;
	HttpSession session = null;

	public String UrlPage(String strURL, int _index) {
		if(strURL == null) {
			return "";
		} else {
			String strPage=null;
			String[] arrSplit=null;
			strURL=strURL.trim();
			if(strURL.indexOf("?") != -1){
				arrSplit=strURL.split("\\?");
				if(strURL.length()>_index) {
					strPage=arrSplit[_index];
				}
			}else{
				strPage = strURL;
			}
			return strPage;
		}
	}

	public TurnPage(HttpServletRequest request,HttpSession session) {
		Object obj = session.getAttribute(GdUserVisitUrlList);
		if(obj!=null){
			userVisitUrl = (LinkedList<HashMap<String,String>>)obj;
		}else{
			userVisitUrl = new LinkedList<HashMap<String,String>>();
		}
		this.request = request;
		this.session = session;
	}

	public void addUrl() {
		String curUri = request.getRequestURI();
		if(curUri.indexOf("HD_portal.jsp")!=-1 || curUri.indexOf("/index.jsp")!=-1){ //首页的话需要清空访问列表
			userVisitUrl.clear();
		} else {
			HashMap<String,String> tpHash = new HashMap<String,String>();
			String preVisitUrl = request.getHeader("Referer");
			String thisUrl = request.getQueryString();
			tpHash.put("full_url",preVisitUrl);
			if(preVisitUrl!=null && preVisitUrl.indexOf("epg_info=")!=-1) { //前一个页面参数中含有epg_info的话，去掉参数
				preVisitUrl = preVisitUrl.substring(0,preVisitUrl.indexOf("?"));
			}

			if(preVisitUrl != null && preVisitUrl.indexOf("portal_key.jsp") != -1){
				preVisitUrl = "";
			}else{
				preVisitUrl = UrlPage(preVisitUrl, 0);
			}

			Enumeration en = request.getParameterNames();
			while(en.hasMoreElements()){
			    String el = String.valueOf(en.nextElement());
			    String elValue = String.valueOf(request.getParameter(el));
			    elValue = elValue == null ? "" : elValue;
			    tpHash.put(el,elValue);
			}
			tpHash.put("url",preVisitUrl);
			userVisitUrl.addLast(tpHash);
			//System.out.println("tpHash=="+tpHash);
			session.setAttribute(GdUserVisitUrlList,userVisitUrl);
		}
	}

	public HashMap<String,String> getPreUrl() {
		HashMap<String,String> tpHash = null;
		if(userVisitUrl.size()>0) {
			tpHash = userVisitUrl.get(userVisitUrl.size()-1);
		}
		return tpHash;
	}
	//dpf---因为参数都是存当前页面的，要想获取上级页面参数，需要取两级
	public HashMap<String,String> getPrePreUrl() {
		HashMap<String,String> tpHash = null;
		if(userVisitUrl.size()>1) {
			tpHash = userVisitUrl.get(userVisitUrl.size()-2);
		}
		return tpHash;
	}

	public HashMap<String,String> removePreUrl() {
		HashMap<String,String> tpHash = null;
		if(userVisitUrl.size()>0) {
			tpHash = userVisitUrl.removeLast();
			session.setAttribute(GdUserVisitUrlList,userVisitUrl);
		}
		return tpHash;
	}

	public void removeAll() {
		userVisitUrl.clear();
		session.setAttribute(GdUserVisitUrlList,userVisitUrl);
		//System.out.println("turn_page==removeAll="+userVisitUrl.size());
	}

	/**
		判断用户是否按返回到当前页
		面的//非null代表是按了返回，null代表不是
	*/
	public HashMap<String,String> isPreUrl() {
		if(userVisitUrl.size()>0) {
			String curUri = this.request.getRequestURI();
			HashMap<String,String> tpHash = getPreUrl();
			String url = tpHash.get("url");

			if(url != null && url.indexOf(curUri)!=-1) { //按了返回键
				tpHash = removePreUrl(); //移除最后一个元素
				return tpHash;
			}else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	判断用户是否按返回到当前页
	面的//非null代表是按了返回，null代表不是
	*/
	public HashMap<String,String> isSdPreUrl() {
		if(userVisitUrl.size()>0) {
			String curUri = this.request.getRequestURI();
			if(curUri!=null && curUri.endsWith("hqjc_index.jsp")) {
				//如果是首页推荐不增加分类divid处理
			} else {
				String divid = this.request.getParameter("divid");
				if(divid!=null)
					curUri = curUri+"?divid="+divid;
			}
			HashMap<String,String> tpHash = getPreUrl();
			String url = tpHash.get("full_url");

			//System.out.println("判断返回：curUri="+curUri);
			//System.out.println("判断返回：url="+url);
			if(url != null && url.indexOf(curUri)!=-1) { //按了返回键
				//System.out.println("返回了");
				tpHash = removePreUrl(); //移除最后一个元素
				return tpHash;
			}else {
				//System.out.println("不是返回");
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	判断用户是否按返回到当前页
	面的//非null代表是按了返回，null代表不是
	*/
	public HashMap<String,String> isCommonsPreUrl() {
		if(userVisitUrl.size()>0) {
			String curUri = this.request.getRequestURI();
			if(request.getQueryString() != null){
				curUri += "?"+request.getQueryString();
			}

			HashMap<String,String> tpHash = getPreUrl();
			String url = tpHash.get("full_url");

			//System.out.println("isCommonsPreUrl=判断返回：curUri="+curUri);
			//System.out.println("isCommonsPreUrl=判断返回：url="+url);
			if(url != null && url.indexOf(curUri)!=-1) { //按了返回键
				//System.out.println("isCommonsPreUrl返回了");
				tpHash = removePreUrl(); //移除最后一个元素
				return tpHash;
			}else {
				//System.out.println("isCommonsPreUrl不是返回");
				return null;
			}
		} else {
			return null;
		}
	}
}
%>